/*
 * @(#) UserInfoContoller.java 1.0  2011-8-5 
 *
 * (c) Copyright 2011 smshen S3H3L3
 *
 * FILENAME    : UserInfoContoller.java
 * PACKAGE     : com.sm.web
 * CREATE DATE : 2011-8-5
 * AUTHOR      : smshen
 */

package com.sm.web;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.sm.model.UserInfo;
import com.sm.service.UserInfoService;

/*******************************************************************************
 * #(c) smshen S3H3L3
 * 
 * 功能说明: 用户管理控制器
 * 
 * 2011-8-5 下午1:45:11 smshen 创建文件
 * 
 * 修改说明: 创建文件
 * 
 * 2011-8-5 下午1:45:11 smshen 修改文件
 * 
 ******************************************************************************/

@Controller
public class UserInfoContoller {

	/**
	 * USER_LIST
	 */
	private static String USER_LIST = "user/userList";

	/**
	 * log 日志
	 */
	Logger log = Logger.getLogger(UserInfoContoller.class);

	/**
	 * userInfoService 用户管理服务
	 */
	@Autowired
	private UserInfoService userInfoService;

	/**
	 * 
	 * 功能 :登录验证
	 * 
	 * 开发：smshen 2011-8-12
	 * 
	 * @param session
	 *            HttpSession
	 * @param userName
	 *            用户名
	 * @param password
	 *            密码
	 * @return modelAndView
	 */
	@RequestMapping("/userInfo/login.do")
	public ModelAndView login(HttpSession session, String userName,
			String password) {
		ModelAndView modelAndView = null;
		UserInfo userInfo = userInfoService.findUserInfoByNameAndPasswd(
				userName, password);
		if (userInfo == null) {
			modelAndView = new ModelAndView("error");
		} else if (userInfo.getRole() == 1) {
			modelAndView = new ModelAndView("manager/manager");
			modelAndView.addObject(userInfo);
			log.info(userInfo.getUserName() + "登录");
		} else if (userInfo.getRole() == 2) {
			modelAndView = new ModelAndView("manager/hello");
			modelAndView.addObject(userInfo);
		} else {
			modelAndView = new ModelAndView("error");
		}
		session.setAttribute("userInfo", userInfo);
		return modelAndView;
	}

	/**
	 * 
	 * 功能 :检查用户名是否存在
	 * 
	 * 开发：smshen 2011-8-12
	 * 
	 * @param userName
	 *            用户名
	 * @return Map<String, Boolean> 是否存在
	 */
	@RequestMapping(value = "/userInfo/checkUserIsExist.do", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Boolean> checkUserIsExist(@RequestBody String userName) {
		return userInfoService.checkUserIsExist(userName);
	}

	/**
	 * 
	 * 功能 :添加用户
	 * 
	 * 开发：smshen 2011-8-12
	 * 
	 * @param userInfo
	 *            用户实体类
	 * @return modelAndView
	 */
	@RequestMapping("/userInfo/addUser.do")
	public ModelAndView addUser(UserInfo userInfo) {
		ModelAndView modelAndView = new ModelAndView(USER_LIST);
		userInfoService.addUser(userInfo);
		return modelAndView;
	}

	/**
	 * 
	 * 功能 :获取全部用户列表
	 * 
	 * 开发：smshen 2011-8-12
	 * 
	 * @param request
	 *            HttpServletRequest
	 * @return ModelAndView
	 */
	@RequestMapping("/userInfo/getAllUserList.do")
	public ModelAndView getAllUserList(HttpServletRequest request) {
		ModelAndView mv = new ModelAndView("json");
		String userInfoJson = userInfoService.getAllUserList(request);
		mv.addObject("json", userInfoJson);
		return mv;
	}

	/**
	 * 
	 * 功能 :删除用户
	 * 
	 * 开发：smshen 2011-8-12
	 * 
	 * @param ids
	 *            id集合
	 * @return ModelAndView
	 */
	@RequestMapping("/userInfo/delUser.do")
	public ModelAndView delUser(String ids) {
		ModelAndView modelAndView = new ModelAndView(USER_LIST);
		for (String id : ids.split(",")) {
			userInfoService.delUser(id);
		}
		return modelAndView;
	}

	/**
	 * 
	 * 功能 :更新用户信息
	 * 
	 * 开发：smshen 2011-8-12
	 * 
	 * @param userInfo
	 *            用户实体类
	 * @return ModelAndView
	 */
	@RequestMapping("/userInfo/updateAndSaveUser.do")
	public ModelAndView updateAndSaveUser(UserInfo userInfo) {
		ModelAndView modelAndView = new ModelAndView(USER_LIST);
		userInfoService.updateAndSaveUser(userInfo);
		return modelAndView;
	}

	/**
	 * 
	 * 功能 :根据用户ID获取用户对象
	 * 
	 * 开发：smshen 2011-8-12
	 * 
	 * @param id
	 *            用户id
	 * @return ModelAndView
	 */
	@RequestMapping("/userInfo/getUserInfoById.do")
	public ModelAndView getUserInfoById(String id) {
		ModelAndView mv = new ModelAndView("user/editUser");
		UserInfo userInfo = userInfoService.getUserInfoById(id);
		mv.addObject(userInfo);
		return mv;
	}

}
